﻿using Microsoft.Graphics.Canvas;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using 纸片人.AttributeUsageFloder;

// https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“空白页”项模板

namespace 纸片人.PageViews
{
    [LastModified("2022-5-12", "create Permutation.xaml")]
    [PageType("cpu", "置换效果")]
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class Permutation : Page
    {
        CanvasBitmap originalBitmap;
        CanvasBitmap permutationBitmap;

        int imageWidth, imageHeight;
        int permutationWidth, permutationHeight;
        Color[] originalColors;
        Color[] permutationColors;
        public Permutation()
        {
            this.InitializeComponent();

            Init();
        }

        void Init()
        {
            selectPicture.Click += async (s, e) =>
           {
               originalBitmap = await Function.SelectImageAsync();
               if (originalBitmap is null) return;
               imageWidth = (int)originalBitmap.SizeInPixels.Width;
               imageHeight = (int)originalBitmap.SizeInPixels.Height;
               originalColors = originalBitmap.GetPixelColors();
               canvas.Invalidate();
           };

            selectPermutationPicture.Click += async (s, e) =>
            {
                permutationBitmap = await Function.SelectImageAsync();
                if (permutationBitmap is null) return;
                permutationColors = permutationBitmap.GetPixelColors();
                permutationWidth = (int)permutationBitmap.SizeInPixels.Width;
                permutationHeight = (int)permutationBitmap.SizeInPixels.Height;
                canvas.Invalidate();
            };

            canvas.Draw += (s, e) =>
            {
                var element = (FrameworkElement)s;
                float effectWidth = (float)element.ActualWidth;
                float effectHeight = (float)element.ActualHeight * 0.7f;

                float previewWidth = effectWidth * 0.5f;
                float previewHeight = (float)element.ActualHeight * 0.3f;

                float previewX = previewWidth;
                float previewY = effectHeight;

                Function.DrawGridGraphics(e.DrawingSession);

                if (originalBitmap != null)
                {
                    var originalTran = Function.CalcutateImageCenteredTransform(previewWidth, previewHeight, imageWidth, imageHeight);
                    originalTran.Source = originalBitmap;
                    e.DrawingSession.DrawImage(originalTran, 0f, previewY);
                }

                if (permutationBitmap != null)
                {
                    var permutationTran = Function.CalcutateImageCenteredTransform(previewWidth, previewHeight, permutationWidth, permutationHeight);
                    permutationTran.Source = permutationBitmap;
                    e.DrawingSession.DrawImage(permutationTran, previewX, previewY);
                }

                if (permutationBitmap != null && originalBitmap != null)
                {
                    var resultColors = PictureFilter.ReplacementEffect(originalColors, permutationColors, imageWidth, imageHeight, permutationWidth, permutationHeight,
                         horRate.Value, verRate.Value, (bool)stretchType.IsChecked, (bool)repeatType.IsChecked);
                    var bitmap = CanvasBitmap.CreateFromColors(canvas, resultColors, imageWidth, imageHeight);
                    var bitmapTran = Function.CalcutateImageCenteredTransform(effectWidth, effectHeight, imageWidth, imageHeight);
                    bitmapTran.Source = bitmap;
                    e.DrawingSession.DrawImage(bitmapTran);
                }
            };

            permutation.Click += (s, e) =>
            {
                canvas.Invalidate();
            };
        }
    }
}
